import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.Format;

import org.mitre.jawb.atlas.AWBDocument;
import org.mitre.jawb.tasks.DefaultInlineExporter;
import org.mitre.jawb.tasks.Exporter;
import org.mitre.jawb.tasks.Task;
import org.mitre.jawb.tasks.generic.GenericTask;


public class ExportingAIFtoTXT {

	public static void main(String[] args) throws MalformedURLException, IOException, URISyntaxException {
		
		String[] folders = {"/home/pjbarrio/Dataset/NaturalDisaster/RelatedData/xml/"};

		Task t = GenericTask.getInstance(new File("/home/pjbarrio/Software/Callisto/tasks/org.mitre.example.naturldisasterlocation.jar"));
		
		Exporter e = new DefaultInlineExporter(t,"sgml");
		
		for (int i = 0; i < folders.length; i++) {
			
			String folderName = folders[i];
			
			File[] files = new File(folderName).listFiles();
			
			for (File file : files) {
				
				String fileName = file.getName();

				if (fileName.endsWith(".xml"))
				
					Export(e,t,fileName,folderName);
				
			}
			
			
		}
		
		
						
	}

	private static void Export(Exporter e, Task t, String fileName, String folderName) throws URISyntaxException, MalformedURLException, IOException {
		
		System.out.println(fileName);
		
		URI file = new URI("file://" + folderName + fileName);
		
		AWBDocument document = AWBDocument.fromAIF(file, t);
		
		new File(folderName + "txt/").mkdir();
		
		URI exportedFile = new URI("file://" + folderName + "txt/" + fileName.replace(".sgml.aif.xml", ""));
		
		e.exportDocument(document, exportedFile);
		
	}
	
}
